package com.j2205.hongzhiyuan.service.impl;


import com.j2205.hongzhiyuan.mapper.DbBakeMapper;
import com.j2205.hongzhiyuan.pojo.DbBack;
import com.j2205.hongzhiyuan.service.DbBackService;
import com.j2205.hongzhiyuan.util.MySqlBackUpUtil;
import com.j2205.hongzhiyuan.util.MybatisUtil;

import java.io.File;
import java.util.Date;
import java.util.List;

/**
 * @ClassName DbBackServiceImpl
 * @Description TODO
 * @Author Zbt
 * @Date 2022/8/18 16:16
 * @Version 1.0
 **/
public class DbBackServiceImpl implements DbBackService {
    @Override
    public List<DbBack> findAll() {
        DbBakeMapper mapper = MybatisUtil.getSession().getMapper(DbBakeMapper.class);
        List<DbBack> dbBacks = mapper.findAll();
        MybatisUtil.clseAll();
        return dbBacks;
    }

    @Override
    public boolean dbBack(String username, String ip) {
        boolean b = MySqlBackUpUtil.backUp("kejiguan", "D:");
        if (b){
            DbBakeMapper mapper = MybatisUtil.getSession().getMapper(DbBakeMapper.class);
            DbBack dbBack = new DbBack();
            dbBack.setBkUser(username);
            dbBack.setBkDate(new Date());
            dbBack.setBkIP(ip);
            dbBack.setBkPath(MySqlBackUpUtil.backFile);
            int i = mapper.addDbBack(dbBack);
            if (i>-1){
                MybatisUtil.commit();
                MybatisUtil.clseAll();
                return true;
            }
            return true;
        }
        return false;
    }

    @Override
    public boolean delete(int bkID) {
        DbBakeMapper mapper = MybatisUtil.getSession().getMapper(DbBakeMapper.class);
        DbBack dbBack = mapper.findOne(bkID);
        File file = new File(dbBack.getBkPath());
        if (file.exists()){
            file.delete();
        }
        int i= mapper.delete(bkID);
        if (i>-1){
            MybatisUtil.commit();
            MybatisUtil.clseAll();
            return true;
        }
        return false;
    }
}
